package org.greenwire.islandharvester.domain;

import java.util.Date;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.greenwire.islandharvester.result.Result;

public class Building implements Harvestable{
	//"id":"104448932","cid":"21332","x":"12","y":"0","z":"0","mirro":"0","event":"0",
	// "waitVisitorNum":"0","deposit":"0","startDeposit":"0","canFind":"1","payRemainder":0
	
	private String id;
	
	/**
	 * 0:有钱
	 * 1:故障
	 */
	private String event;
	
	/**
	 * 当前可用金币
	 */
	private String deposit;
	
	/**
	 * 初始金币
	 */
	private String startDeposit;
	
	/**
	 * 可否到达 0:否 1:可
	 */
	private String canFind;
	
	/**
	 * 结算剩余时间，单位秒
	 */
	private String payRemainder;
	
	private final String COIN_URL = "http://island.hapyfish.com/api/harvestplant?time=";
	
	private final String REPAIR_URL = "http://island.hapyfish.com/api/manageplant?eventType=";
	
	@Override
	public boolean isHarvestable(){
		return StringUtils.isNotBlank(getPayRemainder());
	}
	
	@Override
	public String getType(){
		return "building";
	}
	
	@Override
	public boolean available4HarvestImmediately() {
		if(StringUtils.equals("0", getEvent())){
			return !StringUtils.equals("0", getDeposit()) && StringUtils.equals("0", getPayRemainder());
		} else if (StringUtils.equals("1", getEvent())){
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	public boolean available4HarvestLater(){
		return !StringUtils.equals("0", getPayRemainder());
	}

	@Override
	public String getHarvestUrl(String ownerUid) {
		if(StringUtils.equals("0", getEvent())){
			return COIN_URL + new Date().getTime() +  "&itemId=" + getId();
		} else if (StringUtils.equals("1", getEvent())){
			return REPAIR_URL + "1&itemId=" + getId() + "&ownerUid=" + ownerUid;
		} else {
			throw new IllegalArgumentException("Unrecoginzed event :" + getEvent());
		}
	}
	
	@Override
	public String getNextAvailableTime(){
		return getPayRemainder();
	}

	@Override
	public Result parseResult(String resultStr) {
		// {"status":1,"expChange":3,"coinChange":"21","levelUp":"false","islandLevelUp":false}
		Result result = new Result();
		JSONObject jResult = JSONObject.fromObject(resultStr);
		if(StringUtils.equals("0", getEvent())){ // 收钱
			if (StringUtils.equals("-1", jResult.getString("status"))) {
				result.setSuccess(false);
				result.setResultStr("FAILED! Server return : "
						+ jResult.get("content"));
			} else {
				result.setSuccess(true);
				result.setResultStr(jResult.get("coinChange") + " coins and " + jResult.get("expChange") + " exps received!");
			}
		} else if (StringUtils.equals("1", getEvent())){ // 修理
			JSONObject jsonObject = jResult.getJSONObject("resultVo");
			if (StringUtils.equals("-1", jsonObject.getString("status"))) {
				// serverWord_146 已处理
				result.setSuccess(false);
				result.setResultStr("FAILED! Server return : "
						+ jResult.get("content"));
			} else {
				result.setSuccess(true);
				result.setResultStr(jResult.get("expChange") + " exps received!");
			}
		}
		
		return result;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getEvent() {
		return event;
	}

	public void setEvent(String event) {
		this.event = event;
	}

	public String getDeposit() {
		return deposit;
	}

	public void setDeposit(String deposit) {
		this.deposit = deposit;
	}

	public String getStartDeposit() {
		return startDeposit;
	}

	public void setStartDeposit(String startDeposit) {
		this.startDeposit = startDeposit;
	}

	public String getCanFind() {
		return canFind;
	}

	public void setCanFind(String canFind) {
		this.canFind = canFind;
	}

	public String getPayRemainder() {
		return payRemainder;
	}

	public void setPayRemainder(String payRemainder) {
		this.payRemainder = payRemainder;
	}
}
